//
//  WithDrawHistoryViewController.m
//  lhpc_manager
//
//  Created by 李英杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "WithDrawHistoryViewController.h"
#import "WithDrawHistoryTableViewCell.h"
#import "LHNetworking.h"
#import "WithDrawHistoryModel.h"
#import <YYKit/YYKit.h>

#define PageCount 10

@interface WithDrawHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * mainTableView;

@property (nonatomic, assign) NSInteger page;


@property (nonatomic, strong) NSMutableDictionary * dataDic;

@end

@implementation WithDrawHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"提现记录";
    self.view.backgroundColor = BACKCOLOR;
    
    [self.view addSubview:self.mainTableView];
    
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    
    
    self.mainTableView.mj_footer.hidden = YES;
    [self.mainTableView.mj_header beginRefreshing];
    
}

-(void)getData{
    
    NSDictionary * parameters = @{@"page" : @(self.page),
                                  @"size" : @(PageCount)};
    
    
    LHPC_WEAK_SELF;
    [[LHNetworking shared] POST:LHSetRequestAPI(LHWithDrawHistory) parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        LHPC_STRONG_SELF;
        
        [strongSelf endRefresh];
        
        if (responseObject) {
            [strongSelf hanleResponseObject:responseObject];
        }else{
            
            if (self.page > 0) {
                self.mainTableView.mj_footer.state = MJRefreshStateNoMoreData;
            }
            
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        LHPC_STRONG_SELF;
        [strongSelf endRefresh];
        [HLProgressHUD showHudTipStr:LHNetWorkErrorMessage];
    }];
    
}

-(void)endRefresh{
    if (self.page == 0) {
        [self.mainTableView.mj_header endRefreshing];
    }else{
        [self.mainTableView.mj_footer endRefreshing];
    }
}

-(void)hanleResponseObject:(NSDictionary *)response{
    
    NSArray * array = response[@"data"];
    
    if (self.page == 0) {
        [self.dataDic removeAllObjects];
        if (array.count < PageCount) {
            self.mainTableView.mj_footer.hidden = YES;
        }else{
            self.mainTableView.mj_footer.hidden = NO;
        }
        
    }else{
        
        if (array.count < PageCount) {
            self.mainTableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
    }
    
    
    NSMutableArray * unCompleteArray = [[NSMutableArray alloc]init];
    NSMutableArray * completeArray = [[NSMutableArray alloc]init];
    
    for (NSInteger i = 0; i < array.count; i++) {
        
        WithDrawHistoryModel * model = [WithDrawHistoryModel modelWithDictionary:array[i]];
        
        if (model.status) {
            [completeArray addObject:model];
        }else{
            [unCompleteArray addObject:model];
        }
    }
    
    if (self.page == 0) {
        
        if (unCompleteArray.count > 0) {
            [self.dataDic setObject:unCompleteArray forKey:@"0"];
        }
        if (completeArray.count > 0) {
            [self.dataDic setObject:completeArray forKey:@"1"];
        }
        
    }else{
        
        if (unCompleteArray.count > 0) {
            NSMutableArray * array = self.dataDic[@"0"];
            [array addObjectsFromArray:unCompleteArray];
            [self.dataDic setObject:array forKey:@"0"];
        }
        
        if (completeArray.count > 0) {
            NSMutableArray * array = self.dataDic[@"1"];
            [array addObjectsFromArray:completeArray];
            [self.dataDic setObject:array forKey:@"1"];
        }
        
    }
    
    [self.mainTableView reloadData];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return [self.dataDic allKeys].count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([self.dataDic allKeys].count == 2) {
        if (section == 0) {
            return [self.dataDic[@"0"] count];
        }else{
            return [self.dataDic[@"1"] count];
        }
    }else{
        NSString * str = [[self.dataDic allKeys] firstObject];
        
        return [[self.dataDic valueForKey:str] count];
    }
    
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 35)];
    view.backgroundColor = BACKCOLOR;
    
    UILabel * label = [[UILabel alloc]init];
    [view addSubview:label];
    label.font = [UIFont systemFontOfSize:13];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(view);
        make.left.equalTo(view).offset(15);
        
    }];
    
    if ([self.dataDic allKeys].count == 1) {
        
        NSString * str = [[self.dataDic allKeys] firstObject];
        
        if ([str isEqualToString:@"0"]) {
            label.textColor = ORANGCOLOR;
            label.text = @"提现中";
        }else{
            label.textColor = GRAYCOLOR;
            label.text = @"已完成";
        }
        
    }else{
        
        if (section == 0) {
            label.textColor = ORANGCOLOR;
            label.text = @"提现中";
        }else{
            label.textColor = GRAYCOLOR;
            label.text = @"已完成";
        }
    }
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 75;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WithDrawHistoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"withdrawhistorytableviewcellid" forIndexPath:indexPath];
    
    NSArray * array;
    
    if ([self.dataDic allKeys].count == 2) {
        array = [self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",indexPath.section]];
    }else{
        NSString * str = [[self.dataDic allKeys] firstObject];
        array = [self.dataDic valueForKey:str];
    }
    
    WithDrawHistoryModel * model = array[indexPath.row];
    
    
    [cell setWithDrawHistoryValue:model];
    
    return cell;
    
}

-(UITableView *)mainTableView{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.separatorColor = LINECOLOR;
        _mainTableView.backgroundColor = BACKCOLOR;
        
        [_mainTableView registerClass:[WithDrawHistoryTableViewCell class] forCellReuseIdentifier:@"withdrawhistorytableviewcellid"];
        
        LHPC_WEAK_SELF;
        _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            LHPC_STRONG_SELF;
            
            strongSelf.page = 0;
            strongSelf.mainTableView.mj_footer.state = MJRefreshStateIdle;
            [strongSelf getData];
            
        }];
        
        _mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            LHPC_STRONG_SELF;
            weakSelf.page += 1;
            [strongSelf getData];
            
        }];
        
        _mainTableView.mj_footer.hidden = YES;
        
    }
    return _mainTableView;
}

-(NSMutableDictionary *)dataDic{
    if (!_dataDic) {
        _dataDic = [[NSMutableDictionary alloc]init];
    }
    return _dataDic;
}

@end
